package cn.ekuma.epos.crm.voucher.model;

import cn.ekuma.data.ui.swing.AbstractDTOTableModel;
import cn.ekuma.epos.db.table.crm.voucher.I_Voucher;
import com.openbravo.bean.crm.voucher.Voucher;
import com.openbravo.bean.crm.voucher.VoucherOPEnum;
import com.openbravo.data.loader.serialize.Datas;
import com.openbravo.data.model.Field;
import com.openbravo.format.Formats;

import com.openbravo.pos.base.AppLocal;
import com.openbravo.pos.util.IntegerUtils;
import com.openbravo.pos.util.RoundUtils;

public class VoucherTableModel extends AbstractDTOTableModel<Voucher>{

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		Voucher obj=getObj(rowIndex);
		switch(columnIndex){
		     case  0:
			    return obj.getVoucherNum();
		     case 1:
		    	 return Formats.TIMESTAMP.formatValue(obj.getCreateDate());
		     case 2:
		    	 return getState(obj.getState());
		     case 3:
		    	 return Formats.TIMESTAMP.formatValue(obj.getValidityDate());
		     case 4:
		    	 return RoundUtils.round(obj.getCurLimit());
		     case 5:
		    	 return obj.isVirtual();
		     case 8:
		    	 return obj.getCustomerId();
		     case 9:
		    	 return obj.getMemo();
		}
		return null;
	}
	
   private static int[] doubleInt={4};
	
	@Override
	public Class<?> getColumnClass(int columnIndex) {
		if(IntegerUtils.concat(columnIndex, doubleInt))
			return Double.class;
		return super.getColumnClass(columnIndex);
	}
	
	 private static  final String[] states=new String[]{
	    	AppLocal.getIntString("label.Voucher.state.Unknown"),
	    	AppLocal.getIntString("label.Voucher.state.UseAble"),
	    	AppLocal.getIntString("label.Voucher.state.Back"),
	    	AppLocal.getIntString("label.Voucher.state.Invalid"),
	    	AppLocal.getIntString("label.Voucher.state.Loss"),
	    	AppLocal.getIntString("label.Voucher.state.Used")
	    };

	public static String getState(int state){
		 if(state<states.length)
	    	    return  states[state];
	    	 return String.valueOf(state);
	}
	
	@Override
	public String[] getColumnNames() {
		return new String[]{
				AppLocal.getIntString("label.Voucher.voucherNum"),
				AppLocal.getIntString("label.createTime"),
				AppLocal.getIntString("label.Voucher.state"),
				AppLocal.getIntString("label.Voucher.validityDate"),
				AppLocal.getIntString("label.Voucher.curLimit"),
				AppLocal.getIntString("label.Voucher.virtual"),
				AppLocal.getIntString("label.customer"),
				AppLocal.getIntString("label.notes")
		};
	}
	
	public static Field[] getQBFFields(){
		return new Field[]{
                new Field(I_Voucher.VOUCHERNUM,  Datas.STRING, Formats.STRING,AppLocal.getIntString("label.Voucher.voucherNum")),
                new Field(I_Voucher.CURLIMIT,  Datas.DOUBLE, Formats.DOUBLE,AppLocal.getIntString("label.Voucher.curLimit")),
                new Field(I_Voucher.STATE,     Datas.INT, Formats.INT,AppLocal.getIntString("label.Voucher.state"))
		};
	}
	
	public static String getStoreCardOp(VoucherOPEnum opEnum){
		switch(opEnum){
		case lost:
			return AppLocal.getIntString("label.VoucherOPEnum.lost");
		case recovery:
			return AppLocal.getIntString("label.VoucherOPEnum.recovery");
		case able:
			return AppLocal.getIntString("label.VoucherOPEnum.able");
		case back:
			return AppLocal.getIntString("label.VoucherOPEnum.back");
		case invalid:
			return AppLocal.getIntString("label.VoucherOPEnum.invalid");
		case consumer:
			return AppLocal.getIntString("label.VoucherOPEnum.consumer");
		
		}
		return null;
	}
}
